/*
 * Copyright (C), 2002-2016, 苏宁易购电子商务有限公司
 * FileName: TmController.java
 * Author:   15050536
 * Date:     2016年12月8日 上午10:35:03
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.suning.sawp.web.controller;

import java.security.Principal;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.suning.sawp.constants.LoggerConstants;
import com.suning.sawp.constants.PromSysConstants;
import com.suning.sawp.constants.PromotionErrorConstants;
import com.suning.sawp.constants.TMConstants;
import com.suning.sawp.dto.common.ReturnMsg;
import com.suning.sawp.dto.precharge.CommonRtn;
import com.suning.sawp.intf.promotion.InnerCouponService;
import com.suning.sawp.intf.tm.TimeMachineService;
import com.suning.sawp.service.util.RedisCacheUtils;
import com.suning.sawp.web.exception.MyGsonView;
import com.suning.vgs.follow.remote.store.StoreManEbuyAccount;

/**
 * 时光机 controller
 *
 * @author 15050536 石键平
 */
@Controller
@RequestMapping("/tm/")
public class TmController extends DjBaseController {

	private static final Logger LOGGER = LoggerFactory.getLogger(LoggerConstants.SAWP_SERVICE);

	@Autowired
	private TimeMachineService timeMachineService;
	
	@Autowired
	private InnerCouponService innerCouponService;
	
	@Resource
    private RedisCacheUtils cacheUtils;
	
	@RequestMapping("queryCache.do")
	@ResponseBody
	public String query(String employeeId){
		String key = employeeId;
		if(employeeId != null && employeeId.length() == 8){
			key = TMConstants.TIME_MACHINE_KEY + employeeId;
		}
		String data = cacheUtils.get(key);
		return data;
	}
	
	@RequestMapping("delCache.do")
	@ResponseBody
	public Long delCache(String employeeId){
		String key = employeeId;
		if(employeeId != null && employeeId.length() == 8){
			key = TMConstants.TIME_MACHINE_KEY + employeeId;
		}
		return cacheUtils.del(key);
	}
	
	/**
	 * 
	 * 获取时光机页面需要的所有数据
	 * @param request
	 * @return
	 */
	@RequestMapping("showEmployeeTmInfo.do")
	@ResponseBody
	public String showEmployeeTmInfo(Principal principal){
		String employeeId = principal.getName();
		LOGGER.debug("当前登录用户未:{}",employeeId);
		String json = timeMachineService.findTimeMachineData(employeeId);
		return json;
	}
	
	/**
	 * 
	 * 用户领券
	 *
	 * @param principal
	 * @param activityId 活动id
	 * @param securityKey 加密的key
	 * @param mobile 易购账号
	 * @return
	 */
	@RequestMapping("sendCoupon.do")
	public MyGsonView sendCoupon(Principal principal,String activityId,String securityKey ){
        ReturnMsg<CommonRtn>  ret = new ReturnMsg<CommonRtn>();
        String employeeId = principal.getName();
        LOGGER.info(String.format("TmCouponController.sendCoupon request ,staffId:%s,activityId:%s,securityKey:%s", employeeId,activityId,securityKey));
        try{
        	StoreManEbuyAccount account = timeMachineService.findEbuyNoByEmployeeId(employeeId);
        	if(account == null){
        		 ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E003,
                         PromotionErrorConstants.INNER_COUPON_ERR_MAP);
        		 LOGGER.warn(String.format("TmCouponController.sendCoupon 发券失败,未查到其易购账户。staffId:%s", employeeId));
        		 return setGsonView(ret);
        	}
        	String custNo = account.getCustNo();
            ret = innerCouponService.sendCoupon4TM(custNo, activityId, securityKey, employeeId, PromSysConstants.BONUS_WEIXIN_TM_PROMOTION);
        }catch(Exception e){
            ret.setError(PromotionErrorConstants.INNER_COUPON_ERR_E999,
                    PromotionErrorConstants.INNER_COUPON_ERR_MAP);
            LOGGER.error(String.format("TmCouponController.sendCoupon 发券失败,staffId:%s,activityId:%s,securityKey:%s", employeeId,activityId,securityKey), e);
        }
        return setGsonView(ret);
    }
}
